<template>
  <!-- 工艺参数监控规则明细表单弹窗 -->
  <div>
    <el-dialog
      :close-on-click-modal="false"
      v-model="visibleDialog"
      :title="props.title"
      width="500px"
      draggable
      align-center
      class="maxhDialog1"
      @close="closeDialog"
    >
      <el-form ref="formRef" :model="formData" :rules="rules" label-width="100">
        <el-row>
          <el-col :span="12">
            <el-form-item label="生成工单" prop="isGenerateWorkOrder">
              <el-switch
                v-if="!props.isReadonly"
                v-model="formData.isGenerateWorkOrder"
                :active-value="1"
                :inactive-value="0"
                :disabled="props.isReadonly"
              />
              <el-switch
                v-else
                v-model="formData.isGenerateWorkOrder"
                :active-value="1"
                :inactive-value="0"
                disabled
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="发送邮件" prop="isSendMail">
              <el-switch
                v-if="!props.isReadonly"
                v-model="formData.isSendMail"
                :active-value="1"
                :inactive-value="0"
                :disabled="props.isReadonly"
              />
              <el-switch
                v-else
                v-model="formData.isSendMail"
                :active-value="1"
                :inactive-value="0"
                disabled
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>

      <template #footer>
        <div class="dialog-footer">
          <el-button
            v-if="!props.isReadonly"
            type="primary"
            :loading="loading"
            @click="handleSubmit"
            >确 定</el-button
          >
          <el-button @click="closeDialog">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import request from "@/utils/request";
import { useDictStoreHook } from "@/store/modules/dictionary";
import { useDictionary } from "@/utils/useTable"; // 引入
const dictionaryStore = useDictStoreHook();

defineOptions({
  name: "PSMPARAMITEMCONTRASTBATCHEDIT",
  inheritAttrs: false,
});

const emit = defineEmits(["reset-table", "change-visible"]);

const props = defineProps({
  visible: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
  title: {
    type: String,
    default: () => {
      return "";
    },
  },
  data: {
    type: Object,
    default: () => {
      return {};
    },
  },
  isReadonly: {
    type: Boolean,
    default: () => {
      return false;
    },
  },
});

const visibleDialog = ref(false);
watch(
  () => props.visible,
  (newVal: boolean) => {
    visibleDialog.value = newVal;
    setTimeout(() => {
      formRef.value.clearValidate();
    }, 50);
  }
);

const rules = reactive({
});

const formRef = ref(ElForm);

let formData = reactive({
  isGenerateWorkOrder: 0,
  isSendMail: 0
});

/** 关闭表单弹窗 */
function closeDialog() {
  visibleDialog.value = false;
  emit("change-visible", false);
  resetForm();
}

/** 重置表单 */
function resetForm() {
  formRef.value.resetFields();
  formRef.value.clearValidate();
  formData.isGenerateWorkOrder = 0;
  formData.isSendMail = 0;
}

const loading = ref(false);

/** 工艺参数监控规则明细保存提交 */
function handleSubmit() {
  formRef.value.validate(async (valid: any) => {
    if (valid) {
      loading.value = true;

      const text = "编辑";
      await request({
        url: "/dmpDosPcm/psmparammonitordetail/batchSaveOrderEmail",
        method: "post",
        data: {
          ids: props.data.ids,
          ...formData
        },
      }).finally(() => {
        loading.value = false;
      });
      ElMessage.success(text + `成功`);
      emit("reset-table");
      closeDialog();
    }
  });
}

// 组件销毁时调用
onBeforeUnmount(() => {
  visibleDialog.value = false;
  emit("change-visible", false);
});
</script>
